package com.zking.controller;

import com.zking.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Controller
public class MyController2 {
    @RequestMapping("/login1")
    public String t1(String name, Model model) throws UnsupportedEncodingException {
        if (name == null) {
            return "redirect:/index?error=" + URLEncoder.encode("用户名错误", "utf-8");
        } else if (name.isEmpty()) {
            return "redirect:/index?error=password";
        }
        return "redirect:/index";
    }
    
    @RequestMapping("/login2")
    public String t2(String name, Model model) throws UnsupportedEncodingException {
        if (name == null) {
            model.addAttribute("error", "用户名错误");
            return "redirect:/index";
        } else if (name.isEmpty()) {
            model.addAttribute("error", "密码错误");
            return "redirect:/index";
        }
        return "redirect:/index";
    }
    
    // RedirectAttributes.addAttribute 类似URL参数传递
    // RedirectAttributes.addFlashAttribute 类似Model参数进行传递到另一个请求的Model中【类似session一直存在】
    @RequestMapping("/login3")
    public String t3(String name, RedirectAttributes model) throws UnsupportedEncodingException {
        if (name == null) {
            // model.addAttribute("error", "用户名错误");
            model.addFlashAttribute("error", "用户名错误");
            return "redirect:/index";
        } else if (name.isEmpty()) {
            model.addAttribute("error", "密码错误");
            return "redirect:/index";
        }
        return "redirect:/index";
    }
}
